package com.dog.modules.system.web;

import com.dog.framework.core.model.PageResult;
import com.dog.framework.core.model.ResponseResult;
import com.dog.framework.log.annotation.OperateLog;
import com.dog.framework.log.enums.BusinessType;
import com.dog.modules.system.convert.SysPostConvert;
import com.dog.modules.system.domain.SysPost;
import com.dog.modules.system.api.query.SysPostQuery;
import com.dog.modules.system.api.vo.SysPostVO;
import com.dog.modules.system.service.SysPostService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;


/**
 * 岗位管理
 *
 * @author 阿沐 babamu@126.com
 * <a href="https://maku.net">MAKU</a>
 */
@RestController
@RequestMapping("sys/post")
@Tag(name = "岗位管理")
@AllArgsConstructor
public class SysPostController {
    private final SysPostService sysPostService;

    @GetMapping("page")
    @Operation(summary = "分页")
    public ResponseResult<PageResult<SysPostVO>> page(@RequestBody @Valid SysPostQuery query) {
        PageResult<SysPostVO> page = sysPostService.page(query);

        return ResponseResult.success(page);
    }

    @GetMapping("list")
    @Operation(summary = "列表")
    public ResponseResult<List<SysPostVO>> list() {
        List<SysPostVO> list = sysPostService.getList();

        return ResponseResult.success(list);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    public ResponseResult<SysPostVO> get(@PathVariable("id") Long id) {
        SysPost entity = sysPostService.getById(id);

        return ResponseResult.success(SysPostConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    @OperateLog(businessType = BusinessType.INSERT)
    public ResponseResult<String> save(@RequestBody SysPostVO vo) {
        sysPostService.save(vo);

        return ResponseResult.success();
    }

    @PutMapping
    @Operation(summary = "修改")
    @OperateLog(businessType = BusinessType.UPDATE)
    public ResponseResult<String> update(@RequestBody @Valid SysPostVO vo) {
        sysPostService.update(vo);

        return ResponseResult.success();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @OperateLog(businessType = BusinessType.DELETE)
    public ResponseResult<String> delete(@RequestBody List<Long> idList) {
        sysPostService.delete(idList);

        return ResponseResult.success();
    }
}